package com.wmz.ex.handler;

import com.wmz.constant.BusinessEnum;
import com.wmz.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author wmz
 * @date 2024/7/6 10:47
 * @description 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 处理：业务异步，异常信息用户可以看到
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public Result<String> businessException(BusinessException e) {
        log.info(e.getMessage());
        return Result.fail(BusinessEnum.OPERATION_FAIL.getCode(),e.getMessage());
    }
    /**
     * 服务器运行时异常
     * @param e
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public Result<String> runtimeException(RuntimeException e) {
        log.info(e.getMessage());
        return Result.fail(BusinessEnum.SERVER_ERROR);
    }

    /**
     * 权限不足异常
     * @param e
     * @return
     * @throws AccessDeniedException
     */
    @ExceptionHandler(AccessDeniedException.class)
    public void accessDeniedException(AccessDeniedException e) throws AccessDeniedException  {
        log.info(e.getMessage());
        throw e;
    }
}
